layui.use(['form','layer','jquery','laypage'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery;

    //全选
	form.on('checkbox(allChoose)', function(data){
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
		child.each(function(index, item){
			item.checked = data.elem.checked;
		});
		form.render('checkbox');
	});

	//通过判断文章是否全部选中来确定全选按钮是否选中
	form.on("checkbox(choose)",function(data){
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
		var childChecked = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"]):checked')
		if(childChecked.length == child.length){
			$(data.elem).parents('table').find('thead input#allChoose').get(0).checked = true;
		}else{
			$(data.elem).parents('table').find('thead input#allChoose').get(0).checked = false;
		}
		form.render('checkbox');
	});

	//查询
	$(".search_btn").click(function(){
		var querystr = $(".search_input").val();
		location.href = '/users?querystr=' + querystr;
		layer.msg('查询中，请稍候');
	});

    // 重置用户的密码
    $('.edit_user').click(function(){
        var id = $(this).attr('data-id');
        var index = layer.msg('修改中，请稍候',{icon: 16,time:false,shade:0.8});
        //询问框
        layer.confirm('是否确认重置该用户的支付密码？', {
            btn: ['确认','取消'] //按钮
        }, function(){
            $.ajax({
                url: '/changePsw',
                type: 'GET',
                dataType: 'json',
                timeout: 2000,
                data: {id: id},
                complete: function(){
                    layer.close(index);
                },
                error: function(){
                    // that.prop('checked', orig);
                    layer.alert('参数错误', {icon: 2}, function(index){
                        layer.close(index);
                    });
                },
                success: function(res){
                    if (res.status != 0) {
                        // that.prop('checked', orig);
                        layer.msg(res.msg);
                    } else {
                        layer.msg("已重置");
                        document.location.reload();
                    }
                }
            });
            return false;
        }, function(){
            layer.msg('您已取消该操作！', {
                time: 2000, //3s后自动关闭
                // btn: ['明白', '知道']
            });
            // document.location.reload();
        });
    })
    //删除用户
    $('.delete_user').click(function(){
        var id = $(this).attr('data-id');
        var uid = $(this).attr('data-uid');
        // var index = layer.msg('修改中，请稍候',{icon: 16,time:false,shade:0.8});
        //询问框
        layer.confirm('您确定删除该用户吗？？', {
            btn: ['确认','取消'] //按钮
        }, function(){
            $.ajax({
                url: '/delUser',
                type: 'GET',
                dataType: 'json',
                timeout: 2000,
                data: {id: id,uid: uid},
                complete: function(){
                    layer.close(index);
                },
                error: function(){
                    // that.prop('checked', orig);
                    layer.alert('参数错误', {icon: 2}, function(index){
                        layer.close(index);
                    });
                },
                success: function(res){
                    if (res.status != 0) {
                        // that.prop('checked', orig);
                        layer.msg(res.msg);
                    } else {
                        layer.msg("操作成功");
                        document.location.reload();
                    }
                }
            });
            return false;
        });
    })

});
